﻿@page "/sdk/objects/codegen/a/codegen"
@model WebVella.Erp.Plugins.SDK.Pages.Tools.CodeGenModel
@using WebVella.Erp.Web.Utils;
@{
	Layout = "~/Pages/_AppMaster.cshtml";
	ViewData["Title"] = "Code generation";
}
<wv-page-header color="#dc3545" area-label="Tools" title="Code generation"
				icon-class="fa fa-cloud-download-alt" description="">
	<wv-page-header-actions>
	</wv-page-header-actions>
</wv-page-header>

<p class="alert alert-info ng-scope">This screen generates c# code based on the comparison between two databases. The code than can be used to generate an update script to apply changes in a plugin version automatically. In this way, if you manually create a new field for an entity from the interface, upon comparison, the system will generate the needed code for creating the field automatically.</p>

<wv-validation errors="@Model.Validation.ToErrorList()" message="@Model.Validation.Message"></wv-validation>

<wv-form name="CompareMetaForm" validation-errors="Model.Validation.ToErrorList()" label-mode="Stacked" mode="Form" autocomplete="true">
	<wv-section class="mt-4">
		<wv-row no-gutters="true">
			<wv-column span="11">
				<wv-field-text name="ConnectionString" class="mb-0" value="@Model.ConnectionString" label-text="Original database name or entire connection string (if on different postgre server)"></wv-field-text>
			</wv-column>
			<wv-column span="1" flex-self-align="End" class="pl-2">
				<wv-button type="Submit" text="Generate" color="Primary" is-block="true" icon-class="fa fa-fw fa-cog"></wv-button>
			</wv-column>
		</wv-row>
		<wv-row>
			<wv-column span="4">
				<div class="mb-0 mt-2 form-group wv-field form">
					<label class="control-label label-stacked">Check for META changes in:</label>
				</div>
				<wv-row>
					<wv-column span="-1">
						<wv-field-checkbox name="IncludeEntityMeta" value="@Model.IncludeEntityMeta" text-true="Entity" label-mode="Hidden" class="mb-0"></wv-field-checkbox>
					</wv-column>
					<wv-column span="-1">
						<wv-field-checkbox name="IncludeEntityRelations" value="@Model.IncludeEntityRelations" text-true="Entity Relations" label-mode="Hidden" class="mb-0"></wv-field-checkbox>
					</wv-column>
					<wv-column span="-1">
						<wv-field-checkbox name="IncludeUserRoles" value="@Model.IncludeUserRoles" text-true="User Roles" label-mode="Hidden" class="mb-0"></wv-field-checkbox>
					</wv-column>
					<wv-column span="-1">
						<wv-field-checkbox name="IncludeApplications" value="@Model.IncludeApplications" text-true="Applications & Pages" label-mode="Hidden" class="mb-0"></wv-field-checkbox>
					</wv-column>
				</wv-row>
			</wv-column>
			<wv-column span="4">
				<div class="mb-0 mt-2 form-group wv-field form">
					<label class="control-label label-stacked">Check for RECORD changes in:</label>
				</div>
				<wv-field-multiselect label-mode="Hidden" value="@Model.IncludeRecordsEntityIdList" options="@Model.EntitySelectOptions.ToWvSelectOption()" name="IncludeRecordsEntityIdList"></wv-field-multiselect>
			</wv-column>
			<wv-column span="4">
				<div class="mb-0 mt-2 form-group wv-field form">
					<label class="control-label label-stacked">Check for NN Relation record changes in:</label>
				</div>
				<wv-field-multiselect label-mode="Hidden" value="@Model.IncludeNNRelationIdList" options="@Model.NNRelationsSelectOptions.ToWvSelectOption()" name="IncludeNNRelationIdList"></wv-field-multiselect>
			</wv-column>
		</wv-row>
	</wv-section>
</wv-form>

@if (Model.ShowResults)
{
	<div class="card tabs mt-4">
		<div class="card-header">
			<ul class="nav nav-tabs">
				<li class="nav-item">
					<a class="nav-link active" data-toggle="tab" href="#wv-tab1">Changes</a>
				</li>
				<li class="nav-item">
					<a class="nav-link" data-toggle="tab" href="#wv-tab2">Code</a>
				</li>
			</ul>
		</div>
		<div class="card-body pt-3">
			<div class="tab-content">
				<div class="tab-pane active" id="wv-tab1">
					<wv-grid columns="@Model.Columns" bordered="true" hover="true" has-tfoot="false">
						@foreach (var record in Model.Changes)
						{
							<wv-grid-row>
								<wv-grid-column>
									@Html.Raw(record.Element)
								</wv-grid-column>
								<wv-grid-column>
									@switch (record.Type)
									{
										case "created":
											<span class="go-green label-block">created</span>
											break;
										case "updated":
											<span class="go-orange label-block">updated</span>
											break;
										case "deleted":
											<span class="go-red label-block">deleted</span>
											break;
									}
								</wv-grid-column>

								<wv-grid-column>
									@Html.Raw(record.Name)
								</wv-grid-column>

								<wv-grid-column>
									@if (record.ChangeList != null)
									{
										<ul>
											@foreach (var change in record.ChangeList)
											{
												<li>@Html.Raw(change)</li>
											}
										</ul>
									}

								</wv-grid-column>
							</wv-grid-row>
						}
					</wv-grid>
				</div>
				<div class="tab-pane" id="wv-tab2">
					<wv-field-code value="@Model.Code" mode="Display" language="csharp" label-mode="Hidden"></wv-field-code>
				</div>
			</div>
		</div>
	</div>

}